
package com.mijie.homi.search.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;


/**
 * @author chongxin
 * @since 2013/6/19
 * @version Lucene 4.3.1
 * */
public class LuceneIndex {
	// 索引器
	private IndexWriter writer = null;
	private String file;
	private int curLinenum;
	public LuceneIndex() {
		try {
			//索引文件的保存位置
			Directory dir = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
			//分析器
	  //Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);起作用  
	  
		// 	Analyzer	analyzer=new SimpleAnalyzer(Version.LUCENE_40); 不起作用
			
	  Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_40);//不起作用
		//	Analyzer analyzer = new StopAnalyzer(Version.LUCENE_40);不起作用
	  
		//	Analyzer analyzer=new IKAnalyzer(true); //不起作用
			
		//	Analyzer analyzer=new	PatternAnalyzer("\\\\s");
		
			//配置类
			IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,analyzer);
			iwc.setOpenMode(OpenMode.CREATE);//创建模式 OpenMode.CREATE_OR_APPEND 添加模式
			
			writer = new IndexWriter(dir, iwc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 将要建立索引的文件构造成一个Document对象，并添加一个域"content"
	//private Document getDocument(File f) throws Exception {
	private Document getDocument(String f) throws Exception {
		Document doc = new Document();

//		FileInputStream is = new FileInputStream(f);
//		Reader reader = new BufferedReader(new InputStreamReader(is));
//		//字符串 StringField LongField TextField
//		Field pathField = new StringField("path", f.getAbsolutePath(),Field.Store.YES);
		//Field contenField = new TextField("contents", f);
	    FieldType fieldType = new FieldType();
	    fieldType.setIndexed(true);//set 是否索引
	    fieldType.setStored(true);//set 是否存储
	    fieldType.setTokenized(false);//set 是否分类
		Field contenField = new TextField("contents", f,Field.Store.YES);
		Field LinenumField = new TextField("linenum", String.valueOf( this.curLinenum) ,Field.Store.YES);
	 
		//添加字段
		doc.add(contenField);
	 	doc.add(LinenumField);
		return doc;
	}

	public void writeToIndex() throws Exception {
//		File folder = new File(Constants.INDEX_FILE_PATH);
//		
//		if (folder.isDirectory()) {
//			String[] files = folder.list();
//			for (int i = 0; i < files.length; i++) {
//				File file = new File(folder, files[i]);
//				Document doc = getDocument(file);
//				System.out.println("正在建立索引 : " + file + "");
//				writer.addDocument(doc);
//			}
//		}
		
		fileC0 fc = new fileC0();
		List<String> li1 = fc.fileRead2list(this.file);
		 	for (int i = 0; i < li1.size(); i++) {
			     String line=li1.get(i);
			     line=line.replaceAll(",", " ");
			     this.curLinenum=i+1;
				Document doc = getDocument(line);
				System.out.println("正在建立索引 : " + file + "");
				writer.addDocument(doc);
			}
		 
	}

	public void close() throws Exception {
		writer.close();
	}

	public static void main(String[] args) throws Exception {
		// 声明一个对象
		LuceneIndex indexer = new LuceneIndex();
		indexer.file= "c:\\tonitsi1_deduli_deFei.txt";
		// 建立索引
		Date start = new Date();
		indexer.writeToIndex();
		Date end = new Date();

		System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");

		indexer.close();
	}
}
 